using System;
using System.Collections.Generic;
using System.Linq;

namespace DIS_WEB_Client.Repositories
{
	#region using
    using DIS_WEB_Client.Helpers;
    using DIS_Web_Client.DIS_WS_BusinessLayer;
	#endregion
	public class CustomerRepository
	{
		public List<CustomerDTO> SelectAll()
		{
			try
			{
				return BusService.Instance.Customer_SelectAll().ToList();
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<CustomerDTO>();
		}

		public CustomerDTO SelectByID(int id)
		{
			try
			{
				return BusService.Instance.Customer_SelectByID(id);
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return null;
		}

		public bool Insert(CustomerDTO obj)
		{
			try
			{
				return BusService.Instance.Customer_Insert(obj);
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return false;
		}

		public bool Delete(int id)
		{
			try
			{
                return BusService.Instance.Customer_Delete(id);
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return false;
		}

		public bool Update(CustomerDTO obj)
		{
			try
			{
				return BusService.Instance.Customer_Update(obj);
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return false;
		}

		public List<CustomerDTO> RetrieveByID(int id)
		{
			try
			{
                var item = BusService.Instance.Customer_SelectByID(id);
                if (item != null)
                {
                    return new List<CustomerDTO> { item };
                }
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<CustomerDTO>();
		}

        public int GetTotalPage()
        {
            int RecordCount = GetTotalItem();
            int PageSize = WebConfiguration.NumCustomerPerPage;
            return (RecordCount / PageSize) + ((RecordCount % PageSize == 0) ? 0 : 1);
        }

        public int GetTotalItem()
        {
            try
            {
                return this.SelectAll().Count;
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                    GlobalVariables.Logger.Error("Customer - GetTotalItem: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                else
                    GlobalVariables.Logger.Error("Customer - GetTotalItem: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                return -1;
            }
        }

        public CustomerDTO SelectByUserNameAndPassword(string username, string password)
        {
            try
            {
                return BusService.Instance.Customer_SelectByUserNameAndPassword(username, password);
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    GlobalVariables.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    GlobalVariables.Logger.Error(ex.Message);
                }
            }
            return null;
        }

        public CustomerDTO SelectByUserName(string username)
        {
            try
            {
                return BusService.Instance.Customer_SelectByUserName(username);
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    GlobalVariables.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    GlobalVariables.Logger.Error(ex.Message);
                }
            }
            return null;
        }
	}
}